package net.wantongtech.xa.uploadbasicinfo.dao;

import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.CardBlackListDownloadModel;
import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.dao.impl.GenericResultSetExtractor;
import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.xxdl.InteractionOrgMsgReceive;
import org.joda.time.LocalDateTime;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author MaShibo
 * 2021/3/29 0029
 */
@Repository
public class DownloadReceiveFileDao extends BaseDataTemplate {

    public List<InteractionOrgMsgReceive> queryReceiveMsgByBizCode(int count, int bizCode) {
        final String sql =
                "SELECT *\n" +
                        "  FROM (SELECT a.* \n" +
                        "          FROM tq_interactionorgmsgreceive a\n" +
                        "         WHERE a.bizcode = ?\n" +
                        "         ORDER BY a.createon DESC)\n" +
                        " WHERE rownum <= ?";
        return jdbcTemplate.query(sql, GenericResultSetExtractor.getInstance(InteractionOrgMsgReceive.class),
                bizCode, count);
    }

    public void saveCardBlackHisRecordToSpecTable(final List<CardBlackListDownloadModel> cardBlackListDownloadModels,
                                                  final String version, String tableName) {
        final String baseSql =
                "INSERT INTO %s\n" +
                        "  (listid,\n" +
                        "   inserttime,\n" +
                        "   issuerid,\n" +
                        "   creationtime,\n" +
                        "   cardid,\n" +
                        "   status,\n" +
                        "   TYPE,\n" +
                        "   receivetime,\n" +
                        "   version)\n" +
                        "VALUES\n" +
                        "  (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        getJdbcTemplate().batchUpdate(String.format(baseSql, tableName), new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                CardBlackListDownloadModel model = cardBlackListDownloadModels.get(i);
                ps.setString(1, new RandomGUID().ToRawString());
                ps.setTimestamp(2, new java.sql.Timestamp(LocalDateTime.parse(model.getInsertTime()).toDate().getTime()));
                ps.setString(3, model.getIssuerId());
                ps.setTimestamp(4, new java.sql.Timestamp(LocalDateTime.parse(model.getCreationTime()).toDate().getTime()));
                ps.setString(5, model.getCardId());
                ps.setInt(6, model.getStatus());
                ps.setInt(7, model.getType());
                ps.setTimestamp(8, new java.sql.Timestamp(new Date().getTime()));
                ps.setString(9, version);
            }

            @Override
            public int getBatchSize() {
                return cardBlackListDownloadModels.size();
            }

        });
    }
}
